package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.DehydrationMapper;
import com.zhijian.medical.entity.po.BeforeDehydration;
import com.zhijian.medical.entity.po.Dehydration;
import com.zhijian.medical.entity.vo.request.BeforeDehydrationSearchVo;
import com.zhijian.medical.entity.vo.request.DehydrationSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.DehydrationSearchVo;
import com.zhijian.medical.entity.vo.response.BeforeDehydrationListVo;
import com.zhijian.medical.entity.vo.response.BeforeDehydrationVo;
import com.zhijian.medical.entity.vo.response.DehydrationVo;
import com.zhijian.medical.enums.DehydrationEnum;
import com.zhijian.medical.holder.CurrentUserHolder;
import com.zhijian.medical.service.BeforeDehydrationService;
import com.zhijian.medical.service.DehydrationService;
import com.zhijian.medical.util.AssertUtil;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 脱水记录业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-11-11 11:19:04
 */
@Slf4j
@Service
public class DehydrationServiceImpl implements DehydrationService {

    @Autowired
    private DehydrationMapper mapper;

    @Autowired
    private BeforeDehydrationService beforeDehydrationService;

    @Override
    public Dehydration selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public DehydrationVo selectVoById(Long id) {
        DehydrationVo dehydrationVo = mapper.selectVoById(id);
        if (Objects.isNull(dehydrationVo)) {
            return null;
        }
        BeforeDehydrationVo beforeDehydrationVo = beforeDehydrationService.selectVoById(dehydrationVo.getBeforeId());
        dehydrationVo.setSampleList(Objects.nonNull(beforeDehydrationVo) ? beforeDehydrationVo.getSampleList() : null);
        dehydrationVo.setProcessNumber(Objects.nonNull(beforeDehydrationVo) ? beforeDehydrationVo.getProcessNumber() : null);
        return dehydrationVo;
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(Dehydration dehydration) {
        Date now = new Date();
        dehydration.setCreateTime(now);
        dehydration.setUpdateTime(now);
        return mapper.insert(dehydration) > 0 ? dehydration.getId() : 0L;
    }

    @Override
    public boolean update(Dehydration dehydration) {
        Date now = new Date();
        dehydration.setUpdateTime(now);
        return mapper.updateByPrimaryKey(dehydration) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(Dehydration record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public Integer count(DehydrationSearchVo dehydrationSearchVo) {
        return mapper.count(dehydrationSearchVo);
    }

    @Override
    public List<DehydrationVo> selectList(DehydrationSearchVo dehydrationSearchVo) {
        List<DehydrationVo> dehydrationVos = mapper.selectList(dehydrationSearchVo);
        if (CollectionUtil.isEmpty(dehydrationVos)) {
            return Collections.emptyList();
        }
        dehydrationVos.forEach(dehydrationVo -> {
            BeforeDehydrationVo beforeDehydrationVo = beforeDehydrationService.selectVoById(dehydrationVo.getBeforeId());
            dehydrationVo.setSampleList(Objects.nonNull(beforeDehydrationVo) ? beforeDehydrationVo.getSampleList() : null);
            dehydrationVo.setProcessNumber(Objects.nonNull(beforeDehydrationVo) ? beforeDehydrationVo.getProcessNumber() : null);
        });
        return dehydrationVos;
    }

    @Override
    public Page<DehydrationVo> selectPage(DehydrationSearchVo dehydrationSearchVo) {
        //先直接查总数
        int count = count(dehydrationSearchVo);
        //分页
        Page<DehydrationVo> page = new Page<>(true, count, dehydrationSearchVo.getPageNum(), dehydrationSearchVo.getPageSize());
        dehydrationSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<DehydrationVo> responseVos = this.selectList(dehydrationSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<Dehydration> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Transactional(rollbackFor = Exception.class, value = "localTransactionManager")
    @Override
    public boolean save(DehydrationSaveOrUpdateVo dehydrationSaveOrUpdateVo) {
        //搜一下这个容器有没有已经准备好的数据
        List<BeforeDehydrationListVo> beforeDehydrationListVos = beforeDehydrationService.selectList(BeforeDehydrationSearchVo.builder()
                .containerNumber(dehydrationSaveOrUpdateVo.getContainerNumber())
                .status(DehydrationEnum.WAIT_DEHYDRATION.getCode().byteValue())
                .build());
        AssertUtil.check(CollectionUtil.isEmpty(beforeDehydrationListVos), "此容器没有录入待脱水样本");
        Dehydration dehydration = new Dehydration();
        dehydration.setContainerNumber(dehydrationSaveOrUpdateVo.getContainerNumber());
        dehydration.setDeviceNumber(dehydrationSaveOrUpdateVo.getDeviceNumber());
        dehydration.setBeforeId(beforeDehydrationListVos.get(0).getId());
        dehydration.setOperateUserId(CurrentUserHolder.getUser().getId());
        this.insert(dehydration);
        //把准备表改成脱水中
        beforeDehydrationService.updateByPrimaryKeySelective(BeforeDehydration.builder()
                .id(beforeDehydrationListVos.get(0).getId())
                .status(DehydrationEnum.IN_DEHYDRATION.getCode().byteValue())
                .build());
        return true;
    }

}